package com.cloudcross.ssp.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloudcross.ssp.model.Resources;
import com.cloudcross.ssp.model.Roles;
import com.cloudcross.ssp.service.IRolesService;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * 
 * @author wuqiang.du 2013-11-19
 * @Email: mmm333zzz520@163.com
 * @version 1.0v
 */

public class RolesController extends BaseController {
    @Autowired
    private IRolesService roleService;

    @RequestMapping("list")
    public String list(Model model, Resources menu, String pageNow) {
        return "/back/main/role/list";
    }

    /**
     * 添加角色信息
     * 
     * @param model
     * @param r
     * @param request
     */
    @RequestMapping("/save")
    public void save(Model model, @ModelAttribute("form") Roles r, HttpServletRequest request) {
        try {
            roleService.add(r);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 跑到新增界面
     * 
     * @param model
     * @return
     */
    @RequestMapping("addUI")
    public String addUI() {
        return "/back/main/role/add";
    }

    /**
     * 修改角色信息
     * 
     * @param model
     * @param r
     * @param request
     */
    @RequestMapping("editUI")
    public String editUI(Model model, String roleId) {
        Roles role = roleService.getById(roleId);
        model.addAttribute("role", role);
        return "/back/main/role/edit";
    }

    /**
     * 验证账号是否存在
     * 
     * @author wuqiang.du Email：mmm333zzz520@163.com date：2014-2-19
     * @param name
     * @return
     */
    @RequestMapping("isExist")
    @ResponseBody
    public boolean isExist(String name) {
        Roles role = roleService.isExist(name);
        return role == null;
    }

    /**
     * 删除
     * 
     * @param model
     * @param videoTypeId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Map<String, Object> deleteById(Model model, String ids) {
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    /**
     * 删除角色信息
     * 
     * @param model
     * @param r
     * @param request
     */
    @RequestMapping("/delete")
    public void deleteRoles(Model model, @ModelAttribute("form") Roles r, HttpServletRequest request) {
        roleService.deleteRoles(r.getId());
    }
}
